package com.sedna.recipenote.common.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sedna.recipenote.common.document.AppUser;
import com.sedna.recipenote.common.service.UserService;

@Controller
@RequestMapping(value="open")
public class OpenController {

	private static Logger logger = Logger.getLogger(OpenController.class);

	@Autowired
	UserService userService;

	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String loadLoginPage() {
		return "open/login";
	}
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logout() {
		logger.info("Logged out...");
		return "open/login";
	}

	@RequestMapping(value="/signup", method=RequestMethod.GET)
	public String loadSignupPage(Model model) {
		logger.info("loading signup page");
		AppUser appUser = new AppUser();
		model.addAttribute("appUser", appUser);
		return "open/signup";
	}

	@RequestMapping(value = "createUser", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> createUser(AppUser appUser) {
		userService.signup(appUser);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("message", "User has been created. Please sign in now");
		return modelMap;
	}

	@RequestMapping(value="/loginFailed")
	public String loadLoginFailedPage() {
		logger.info("Login failed...");
		return "open/login";
	}
}
